﻿Imports Model
Imports Model.Geometry
Imports Model.Midline
Imports Helper
Imports Model.Result
Imports System.Windows.Forms

Public Module Apply

    Public Function DrawCircleResultToCAD(ByVal result As List(Of CircleResult), ByRef pro As ProgressBar, ByVal propath As String, ByVal time As String)
        Dim flag As Integer = 0
        Dim flag2 As Double = 0.0
        CADHelper.GetAcadApplication(propath)
        CADHelper.initDrawSpace()
        Dim dcwc = New DrawCADWithCircle()
        For i As Integer = 0 To result.Count - 1
            flag = (i \ 2)
            flag2 = 13.69 * flag
            pro.Value = Math.Round((i + 1) * 100 / result.Count)

            dcwc.cenpt = result(i).fitcircle.center
            dcwc.radius = result(i).fitcircle.radius
            dcwc.designcenpt = result(i).design_center
            dcwc.surveycenpt = result(i).suvery_center
            dcwc.stanele = result(i).stand_ele
            dcwc.designmaxele = result(i).design_max
            dcwc.designminele = result(i).design_min
            dcwc.drawpoint = result(i).false_ptlist
            dcwc.surveymaxele = result(i).max_ele
            dcwc.surveyminele = result(i).min_ele
            dcwc.mil = result(i).stand_mil

            Dim tag As Integer = (i Mod 2)
            If tag = 0 Then
                dcwc.cenpt.p_y -= flag2
                For j = 0 To dcwc.drawpoint.Count - 1
                    dcwc.drawpoint(j).p_y -= flag2
                    CADHelper.DrawPoint(dcwc.drawpoint(j))

                Next
            ElseIf tag = 1 Then
                dcwc.cenpt.p_x += 14
                dcwc.cenpt.p_y -= flag2
                For j = 0 To dcwc.drawpoint.Count - 1
                    dcwc.drawpoint(j).p_x += 14
                    dcwc.drawpoint(j).p_y -= flag2
                    CADHelper.DrawPoint(dcwc.drawpoint(j))
                Next
            End If

            CADHelper.DrawCircle(dcwc.cenpt, dcwc.radius)

            Dim rlinepos As New Point3d()
            rlinepos.p_x = dcwc.cenpt.p_x + dcwc.radius
            rlinepos.p_y = dcwc.cenpt.p_y
            CADHelper.DrawLine(dcwc.cenpt, rlinepos)
            Dim kpos As New Point3d()
            kpos.p_x = dcwc.cenpt.p_x - 1.8
            kpos.p_y = dcwc.cenpt.p_y - 3.4
            CADHelper.AddText(kpos, dcwc.mil, 0.5)
            Dim rtextpos As New Point3d()
            rtextpos.p_x = dcwc.cenpt.p_x + 0.4
            rtextpos.p_y = dcwc.cenpt.p_y + 0.2
            CADHelper.AddText(rtextpos, "r=" + Math.Round(dcwc.radius, 3).ToString() + "m", 0.2)
            Dim stanline_spt As New Point3d()
            Dim stanline_ept As New Point3d()
            stanline_spt.p_y = dcwc.stanele - flag2
            stanline_spt.p_x = dcwc.cenpt.p_x - 1.6
            stanline_ept.p_y = dcwc.stanele - flag2
            stanline_ept.p_x = dcwc.cenpt.p_x + 1.6
            CADHelper.DrawLine(stanline_spt, stanline_ept)
            Dim stantextpos As New Point3d()
            stantextpos.p_x = stanline_spt.p_x + 0.1
            stantextpos.p_y = stanline_spt.p_y + 0.2
            CADHelper.AddText(stantextpos, "轨面设计标高：" + Math.Round(dcwc.stanele, 3).ToString() + "m", 0.2)

            Dim alltextpos As New Point3d()
            alltextpos.p_x = dcwc.cenpt.p_x + 3.1
            alltextpos.p_y = dcwc.cenpt.p_y + dcwc.radius

            alltextpos.p_y -= 0.6
            CADHelper.AddText(alltextpos, "设计", 0.2)
            alltextpos.p_y -= 0.6
            CADHelper.AddText(alltextpos, "隧道中心设计坐标：" + Math.Round(dcwc.designcenpt.p_x, 3).ToString() + "，" + Math.Round(dcwc.designcenpt.p_y, 3).ToString(), 0.2)
            alltextpos.p_y -= 0.6
            CADHelper.AddText(alltextpos, "隧道顶设计标高：" + Math.Round(dcwc.designmaxele, 3).ToString(), 0.2)
            alltextpos.p_y -= 0.6
            CADHelper.AddText(alltextpos, "隧道底设计标高：" + Math.Round(dcwc.designminele, 3).ToString(), 0.2)
            alltextpos.p_y -= 0.6
            CADHelper.AddText(alltextpos, "实测", 0.2)
            alltextpos.p_y -= 0.6
            CADHelper.AddText(alltextpos, "隧道中心实测坐标：" + Math.Round(dcwc.surveycenpt.p_x, 3).ToString() + "，" + Math.Round(dcwc.surveycenpt.p_y, 3).ToString(), 0.2)
            alltextpos.p_y -= 0.6
            CADHelper.AddText(alltextpos, "隧道顶实测标高：" + Math.Round(dcwc.surveymaxele, 3).ToString(), 0.2)
            alltextpos.p_y -= 0.6
            CADHelper.AddText(alltextpos, "隧道底实测标高：" + Math.Round(dcwc.surveyminele, 3).ToString(), 0.2)

            Dim alignp1, alignp2, alignp3, alignloc, alignloc2 As New Point3d()
            alignp1.p_x = dcwc.cenpt.p_x
            alignp1.p_y = dcwc.cenpt.p_y + dcwc.radius
            alignp2.p_x = dcwc.cenpt.p_x
            alignp2.p_y = dcwc.stanele - flag2
            alignp3.p_x = dcwc.cenpt.p_x
            alignp3.p_y = dcwc.cenpt.p_y - dcwc.radius
            alignloc.p_x = dcwc.cenpt.p_x - 3.3
            alignloc.p_y = (alignp1.p_y + alignp2.p_y) / 2
            alignloc2.p_x = dcwc.cenpt.p_x - 3.3
            alignloc2.p_y = (alignp2.p_y + alignp3.p_y) / 2
            CADHelper.DrawLine(alignp1, alignp2)
            CADHelper.DrawLine(alignp2, alignp3)
            CADHelper.AddDimAligned(alignp1, alignp2, alignloc)
            CADHelper.AddDimAligned(alignp2, alignp3, alignloc2)
        Next
        CADHelper.finishDrawSpace(time)
        Return Nothing
    End Function

    Public Function WriteDataToExcel(ByVal result As List(Of CircleResult), ByRef pro As ProgressBar, ByVal propath As String, ByVal time As String)
        EXCELHelper.GetExcelApplicationCircle(propath)
        For i = 0 To result.Count - 1
            pro.Value = Math.Round((i + 1) * 100 / result.Count)
            Threading.Thread.Sleep(3000)
            EXCELHelper.Writedata(result(i), i)
        Next
        EXCELHelper.FinishWrite(time)
        Return Nothing
    End Function

    Public Function WriteDataToExcel(ByVal result As List(Of HorseResult), ByRef pro As ProgressBar, ByVal propath As String, ByVal time As String)
        EXCELHelper.GetExcelApplicationHorse(propath)
        For i = 0 To result.Count - 1
            pro.Value = Math.Round((i + 1) * 100 / result.Count)
            Threading.Thread.Sleep(3000)
            EXCELHelper.Writedata(result(i), i)
        Next
        EXCELHelper.FinishWrite(time)
        Return Nothing
    End Function

    Public Function DrawHorseResultToCAD(ByVal result As List(Of HorseResult), ByRef pro As ProgressBar, ByVal propath As String, ByVal time As String)
        Dim flag As Integer = 0
        Dim flag2 As Double = 0.0
        CADHelper.GetAcadApplication(propath)
        CADHelper.initDrawSpace()
        Dim dcwh = New DrawCADWithHorse()
        For i = 0 To result.Count - 1
            flag = (i \ 2)
            flag2 = 13.69 * flag
            pro.Value = Math.Round((i + 1) * 100 / result.Count)

            dcwh.cenpt_up = result(i).fitcircle_up.center
            dcwh.cenpt_down = result(i).fitcircle_down.center
            dcwh.radius_up = result(i).fitcircle_up.radius
            dcwh.radius_down = result(i).fitcircle_down.radius
            dcwh.designcenpt = result(i).design_center
            dcwh.surveycenpt = result(i).suvery_center
            dcwh.stanele = result(i).stand_ele
            dcwh.designmaxele = result(i).design_max
            dcwh.designminele = result(i).design_min
            dcwh.drawpoint = result(i).false_ptlist
            dcwh.surveymaxele = result(i).max_ele
            dcwh.surveyminele = result(i).min_ele
            dcwh.mil = result(i).stand_mil



            Dim tag As Integer = (i Mod 2)
            If tag = 0 Then
                dcwh.cenpt_down.p_y -= flag2
                dcwh.cenpt_up.p_y -= flag2
                For j = 0 To dcwh.drawpoint.Count - 1
                    dcwh.drawpoint(j).p_y -= flag2
                    CADHelper.DrawPoint(dcwh.drawpoint(j))

                Next
            ElseIf tag = 1 Then
                dcwh.cenpt_down.p_x += 14
                dcwh.cenpt_down.p_y -= flag2
                dcwh.cenpt_up.p_x += 14
                dcwh.cenpt_up.p_y -= flag2
                For j = 0 To dcwh.drawpoint.Count - 1
                    dcwh.drawpoint(j).p_x += 14
                    dcwh.drawpoint(j).p_y -= flag2
                    CADHelper.DrawPoint(dcwh.drawpoint(j))
                Next
            End If

            '画上下两个圆
            CADHelper.DrawArc(dcwh.cenpt_up, dcwh.radius_up, DegreeToRad(37), DegreeToRad(143))
            CADHelper.DrawArc(dcwh.cenpt_down, dcwh.radius_down, DegreeToRad(251), DegreeToRad(289))

            'Dim rlinepos_up As New Point3d()
            'rlinepos_up.p_x = dcwh.cenpt_up.p_x
            'rlinepos_up.p_y = dcwh.cenpt_up.p_y + dcwh.radius_up
            'CADHelper.DrawLine(dcwh.cenpt_up, rlinepos_up)

            'Dim rlinepos_down As New Point3d()
            'rlinepos_down.p_x = dcwh.cenpt_down.p_x
            'rlinepos_down.p_y = dcwh.cenpt_down.p_y - dcwh.radius_down
            'CADHelper.DrawLine(dcwh.cenpt_down, rlinepos_down)

            Dim kpos As New Point3d()
            kpos.p_x = dcwh.cenpt_down.p_x - 1.8
            kpos.p_y = dcwh.cenpt_up.p_y - 3.4
            CADHelper.AddText(kpos, dcwh.mil, 0.5)
            'Dim rtextpos As New Point3d()
            'rtextpos.p_x = dcwh.cenpt.p_x + 0.4
            'rtextpos.p_y = dcwh.cenpt.p_y + 0.2
            'CADHelper.AddText(rtextpos, "r=" + Math.Round(dcwh.radius, 3).ToString() + "m", 0.2)
            Dim stanline_spt As New Point3d()
            Dim stanline_ept As New Point3d()
            stanline_spt.p_y = dcwh.stanele - flag2
            stanline_spt.p_x = dcwh.cenpt_down.p_x - 1.6
            stanline_ept.p_y = dcwh.stanele - flag2
            stanline_ept.p_x = dcwh.cenpt_down.p_x + 1.6
            CADHelper.DrawLine(stanline_spt, stanline_ept)
            Dim stantextpos As New Point3d()
            stantextpos.p_x = stanline_spt.p_x + 0.1
            stantextpos.p_y = stanline_spt.p_y + 0.2
            CADHelper.AddText(stantextpos, "轨面设计标高：" + Math.Round(dcwh.stanele, 3).ToString() + "m", 0.2)

            Dim alltextpos As New Point3d()
            alltextpos.p_x = dcwh.cenpt_up.p_x + 3.1
            alltextpos.p_y = dcwh.cenpt_up.p_y + dcwh.radius_up

            alltextpos.p_y -= 0.6
            CADHelper.AddText(alltextpos, "设计", 0.2)
            alltextpos.p_y -= 0.6
            CADHelper.AddText(alltextpos, "隧道中心设计坐标：" + Math.Round(dcwh.designcenpt.p_x, 3).ToString() + "，" + Math.Round(dcwh.designcenpt.p_y, 3).ToString(), 0.2)
            alltextpos.p_y -= 0.6
            CADHelper.AddText(alltextpos, "隧道顶设计标高：" + Math.Round(dcwh.designmaxele, 3).ToString(), 0.2)
            alltextpos.p_y -= 0.6
            CADHelper.AddText(alltextpos, "隧道底设计标高：" + Math.Round(dcwh.designminele, 3).ToString(), 0.2)
            alltextpos.p_y -= 0.6
            CADHelper.AddText(alltextpos, "实测", 0.2)
            alltextpos.p_y -= 0.6
            CADHelper.AddText(alltextpos, "隧道中心实测坐标：" + Math.Round(dcwh.surveycenpt.p_x, 3).ToString() + "，" + Math.Round(dcwh.surveycenpt.p_y, 3).ToString(), 0.2)
            alltextpos.p_y -= 0.6
            CADHelper.AddText(alltextpos, "隧道顶实测标高：" + Math.Round(dcwh.surveymaxele, 3).ToString(), 0.2)
            alltextpos.p_y -= 0.6
            CADHelper.AddText(alltextpos, "隧道底实测标高：" + Math.Round(dcwh.surveyminele, 3).ToString(), 0.2)

            Dim alignp1, alignp2, alignp3, alignp4, alignloc, alignloc2 As New Point3d()
            alignp1.p_x = dcwh.cenpt_up.p_x
            alignp1.p_y = dcwh.cenpt_up.p_y + dcwh.radius_up
            alignp2.p_x = dcwh.cenpt_up.p_x
            alignp2.p_y = dcwh.stanele - flag2
            alignp3.p_x = dcwh.cenpt_down.p_x
            alignp3.p_y = dcwh.cenpt_down.p_y - dcwh.radius_down
            alignp4.p_x = dcwh.cenpt_down.p_x
            alignp4.p_y = dcwh.stanele - flag2
            alignloc.p_x = dcwh.cenpt_up.p_x - 3.3
            alignloc.p_y = (alignp1.p_y + alignp2.p_y) / 2
            alignloc2.p_x = dcwh.cenpt_down.p_x - 3.3
            alignloc2.p_y = (alignp2.p_y + alignp3.p_y) / 2
            CADHelper.DrawLine(alignp1, alignp2)
            CADHelper.DrawLine(alignp4, alignp3)
            CADHelper.AddDimAligned(alignp1, alignp2, alignloc)
            CADHelper.AddDimAligned(alignp4, alignp3, alignloc2)
        Next
        CADHelper.finishDrawSpace(time)
        Return Nothing
    End Function

    Public Function DrawRectangleResultToCAD(ByVal result As List(Of RectangleResult), ByRef pro As ProgressBar, ByVal propath As String, ByVal time As String)
        Dim flag As Integer = 0
        Dim flag2 As Double = 0.0
        CADHelper.GetAcadApplication(propath)
        CADHelper.initDrawSpace()
        'Dim dcwh = New DrawCADWithRectangle()
        For i = 0 To result.Count - 1
            Dim dcwh = New DrawCADWithRectangle()
            flag = (i \ 2)
            flag2 = 13 * flag
            pro.Value = Math.Round((i + 1) * 100 / result.Count)

            dcwh.designcenpt = result(i).design_center
            dcwh.stanele = result(i).stand_ele
            dcwh.designmaxele = result(i).design_max
            dcwh.designminele = result(i).design_min
            dcwh.drawpoint = result(i).false_ptlist
            dcwh.surveymaxele = result(i).max_ele
            dcwh.surveyminele = result(i).min_ele
            dcwh.mil = result(i).stand_mil
            dcwh.center.p_y = dcwh.stanele
            dcwh.difleft0 = result(i).difCeGG
            dcwh.difleft21 = result(i).difCeGG_21
            dcwh.difleft42 = result(i).difCeGG_42
            dcwh.difleftup = result(i).difmax_ele
            dcwh.difleftdown = result(i).difmin_ele
            dcwh.difleftzth = result(i).dif_zth
            dcwh.difleftztv = result(i).dif_ztv

            Dim tag As Integer = (i Mod 2)
            If tag = 0 Then
                dcwh.center.p_y -= flag2
                For j = 0 To dcwh.drawpoint.Count - 1
                    dcwh.drawpoint(j).p_y -= flag2
                    CADHelper.DrawPoint(dcwh.drawpoint(j))
                Next
            ElseIf tag = 1 Then
                dcwh.center.p_x += 12
                dcwh.center.p_y -= flag2
                For j = 0 To dcwh.drawpoint.Count - 1
                    dcwh.drawpoint(j).p_x += 12
                    dcwh.drawpoint(j).p_y -= flag2
                    CADHelper.DrawPoint(dcwh.drawpoint(j))
                Next
            End If

            '绘制里程文字
            Dim kpos As New Point3d()
            kpos.p_x = dcwh.center.p_x - 1.0
            kpos.p_y = dcwh.center.p_y - 1.1
            CADHelper.AddText(kpos, dcwh.mil, 0.3)

            Dim elept1, elept2 As New Point3d()
            elept1.p_x = dcwh.center.p_x - 1.6
            elept1.p_y = dcwh.center.p_y
            elept2.p_x = dcwh.center.p_x + 1.6
            elept2.p_y = dcwh.center.p_y
            CADHelper.DrawLine(elept1, elept2)
            '绘制标注
            Dim alignpup, alignpdown, alignlocup, alignlocdown As New Point3d()
            'up
            alignpup.p_x = dcwh.center.p_x
            alignpup.p_y = dcwh.center.p_y + dcwh.difleftup
            alignlocup.p_x = dcwh.center.p_x - 3
            alignlocup.p_y = (alignpup.p_y + dcwh.center.p_y) / 2
            'down
            alignpdown.p_x = dcwh.center.p_x
            alignpdown.p_y = dcwh.center.p_y - dcwh.difleftdown
            alignlocdown.p_x = dcwh.center.p_x - 3
            alignlocdown.p_y = (alignpdown.p_y + dcwh.center.p_y) / 2

            CADHelper.DrawLine(alignpup, dcwh.center)
            CADHelper.DrawLine(alignpdown, dcwh.center)
            CADHelper.AddDimAligned(alignpup, dcwh.center, alignlocup)
            CADHelper.AddDimAligned(dcwh.center, alignpdown, alignlocdown)

            Dim alignp0, alignp210, alignp211, alignp420, alignp421, alignloc0, alignloc21, alignloc42 As New Point3d()
            alignp0.p_x = dcwh.center.p_x - dcwh.difleft0
            alignp0.p_y = dcwh.center.p_y
            alignloc0.p_x = (alignp0.p_x + dcwh.center.p_x) / 2
            alignloc0.p_y = dcwh.center.p_y
            CADHelper.AddDimAligned(alignp0, dcwh.center, alignloc0)
            alignp210.p_x = dcwh.center.p_x - dcwh.difleft21
            alignp210.p_y = dcwh.center.p_y + 2.1
            alignp211.p_x = dcwh.center.p_x
            alignp211.p_y = dcwh.center.p_y + 2.1
            alignloc21.p_x = (alignp210.p_x + dcwh.center.p_x) / 2
            alignloc21.p_y = alignp210.p_y
            CADHelper.AddDimAligned(alignp210, alignp211, alignloc21)
            alignp420.p_x = dcwh.center.p_x - dcwh.difleft42
            alignp420.p_y = dcwh.center.p_y + 4.2
            alignp421.p_x = dcwh.center.p_x
            alignp421.p_y = dcwh.center.p_y + 4.2
            alignloc42.p_x = (alignp420.p_x + dcwh.center.p_x) / 2
            alignloc42.p_y = alignp420.p_y
            CADHelper.AddDimAligned(alignp420, alignp421, alignloc42)

            Dim alignpzth1, alignpzth2, alignpztv1, alignpztv2, alignloczth, alignlocztv As New Point3d()
            alignpzth1.p_x = dcwh.center.p_x
            alignpzth1.p_y = dcwh.center.p_y + 1.5
            alignpzth2.p_x = dcwh.center.p_x + dcwh.difleftzth
            alignpzth2.p_y = dcwh.center.p_y + 1.5
            alignloczth.p_x = (alignpzth1.p_x + alignpzth2.p_x) / 2
            alignloczth.p_y = alignpzth1.p_y + 1.1
            CADHelper.AddDimAligned(alignpzth1, alignpzth2, alignloczth)
            alignpztv1.p_x = dcwh.center.p_x
            alignpztv1.p_y = dcwh.center.p_y
            alignpztv2.p_x = dcwh.center.p_x
            alignpztv2.p_y = dcwh.center.p_y + dcwh.difleftztv
            alignlocztv.p_x = alignpztv1.p_x + 0.5
            alignlocztv.p_y = (alignpztv1.p_y + alignpztv2.p_y) / 2
            CADHelper.AddDimAligned(alignpztv1, alignpztv2, alignlocztv)

        Next
        CADHelper.finishDrawSpace(time)
        Return Nothing
    End Function

    Public Function WriteDataToExcel(resultlist As List(Of RectangleResult), progressBar As ProgressBar, pubpath As Object, currtime As String)
        EXCELHelper.GetExcelApplicationRectangle(pubpath)
        For i = 0 To resultlist.Count - 1
            progressBar.Value = Math.Round((i + 1) * 100 / resultlist.Count)
            Threading.Thread.Sleep(3000)
            EXCELHelper.Writedata(resultlist(i), i)
        Next
        EXCELHelper.FinishWrite(currtime)
        Return Nothing
    End Function


End Module
